package com.googlecode.utils
{
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.core.math.Number3D;
	import org.papervision3d.core.math.Plane3D;
	import org.papervision3d.view.Viewport3D;
	
	public class ScreenTo3D
	{
		private var viewport:Viewport3D;
		private var p:Plane3D;
		
		public function ScreenTo3D(v:Viewport3D)
		{
			viewport = v;
			
			//Create a Plane3D object. Initially in zx
			p = new Plane3D();
			p.setNormalAndPoint(new Number3D(0,1,0), new Number3D(0,0,0));
		}
		
		public function get3DPoint(cam:Camera3D):Number3D
		{
			var mX:Number = viewport.containerSprite.mouseX;
			var mY:Number = viewport.containerSprite.mouseY;
			
			var ray:Number3D = cam.unproject(mX, mY);
			
			//Make our camera position accessible
			var cameraPosition:Number3D = new Number3D(cam.x, cam.y, cam.z);
			
			//Get the world position of our ray
			ray = Number3D.add(ray, cameraPosition);
				
			var intersect:Number3D = p.getIntersectionLineNumbers(cameraPosition, ray);
			
			return intersect;
		}
		
		public function setViewPlane(normal:Number3D, pt:Number3D=null):void
		{
			if(!pt){
				pt = new Number3D(0,0,0);
			}
			p.setNormalAndPoint(normal, pt);
		}
		
	}
}